var $article = {
	
	//载入列表模板
	load_list: function(callback)
	{
		$("#article").load("article_list.html", function(){
			if($.isFunction(callback)) callback();
		});
	},
	
	//取得数据列表
	get_list: function($sortid, $title, $page)
	{
		var $pagesize = 30;
		$sortid = parseInt($sortid);
		var $data = {}, $where="";
		if(!isNaN($sortid))
		{
			$data.sortid = $sortid;
			$where += " AND sortid = :sortid";
		}
		if($title)
		{
			$data.title = "%"+$title+"%";
			$where += " AND title LIKE :title";
		}
		
		var $total = $db.fetch_row("SELECT COUNT(*) AS total FROM law_article WHERE 1"+$where, $data);
		$total = $total.total;
		if(!$total)
		{
			alert("没有找到任何数据");
			return false;
		}
		
		var $sql = "SELECT id, sortid, title, addtime FROM law_article WHERE 1"+$where+get_limit($page, $pagesize);
		var $result = $db.fetch_all($sql, $data);
		
		delete $where, $data;
		
		//只有一条结果，直接显示该条结果内容
		if($result.length == 1)
		{
			$article.get_data($result[0].id);
			return true;
		}
		
		$article.load_list(function(){
			var $sort_list = $sort.get_data(), $html = "";
			$.each($result, function(k, v){
				$html += "<tr>";
				$html += "<td>[<a href='###' class='sort' sortid='"+v.sortid+"'>"+ $sort_list[v.sortid] +"</a>]</td>";
				$html += "<td><a href='###' class='title' aid='"+ v.id +"'>"+ v.title +"</a></td>";
				$html += "<td>"+ from_timestamp(v.addtime).format("yyyy-MM-dd hh:mm") +"</td>";
				$html += "</tr>";
			});
			var $article_list = $("#article_list > tbody").html($html);
			$article_list.find("a.sort").click(function(){
				$article.get_list($(this).attr("sortid"));
			});
			$article_list.find("a.title").click(function(){
				$article.get_data($(this).attr("aid"));
			});
			
			//分页
			var $pager = new pager($total, $page, $pagesize, function(p){
				$article.get_list($sortid, $title, p);
			});
			$("#pager").html($pager.get_html());
		});
		location.href = "#";
		return true;
	},
	
	//取得一条数据内容
	get_data: function($id)
	{
		var $sql = "SELECT * FROM law_article WHERE id = :id";
		var $data = $db.fetch_row($sql, {id:$id});
		$("#article").load("article_content.html", function(){
			//处理内容
			var $content = $data.content;
			$content = $content.replace(/ /g, "&nbsp;&nbsp;");
			$content = $content.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
			$content = $content.replace(/\n/g, "<br />");
			$content = $content.replace(/\[b\]/ig, "<b>");
			$content = $content.replace(/\[\/b\]/ig, "</b>");
			$content = $content.replace(/\[i\]/ig, "<i>");
			$content = $content.replace(/\[\/i\]/ig, "</i>");
			$content = $content.replace(/\[u\]/ig, "<u>");
			$content = $content.replace(/\[\/u\]/ig, "</u>");
			$content = $content.replace(/\[center\]/ig, "<center>");
			$content = $content.replace(/\[\/center\]/ig, "</center>");

			
			$("#article_content h2").html($data.title);
			$("#content_addtime").html(from_timestamp($data.addtime).format("yyyy-MM-dd hh:mm"));
			$("#content_content").html($content);
		});
	}
	
	
};